<?php
require_once($application_folder."/controllers/navigator.php");
class Article extends Navigator{

    function __construct(){
        parent::__construct();
        $this->load->model('articlem','art');
    }

    function index(){
        show_404();
    }

    function new_form(){

        $this->navigate('article/new_form');

    }

    function media_form(){        

        $action = FALSE;
        $msg = '';

        if($this->valid_new_form()){

            $this->navigate('article/media_form',$_POST);

            $action = TRUE;
            $msg = 'Sledeći korak';

        }else{

            $msg = validation_errors();

            echo json_encode(array('action' => $action, 'msg' => $msg));

        }

    } 

    function preview_form(){        

        $action = FALSE;
        $msg = '';

        if($this->valid_media_form()){

            $this->navigate('article/preview_form',$_POST);

            $action = TRUE;
            $msg = 'Uspješna akcija';

        }else{

            $msg = validation_errors();

            echo json_encode(array('action' => $action, 'msg' => $msg));

        }   

    }

    function create(){

        $data = $_POST;

        $action = FALSE;
        $msg = '';

        if($this->valid_media_form()){


            $data['datepublish'] = time();
            $data['status'] = 0;
            $data['priority'] = 4;
            $data['user_id'] = $this->session->userdata('id');

            /* INSERT MEDIA */
            $this->db->insert('media',array('type'=>$data['_type'], 'filepath'=> $data['_filepath']));        

            $mediaid = $this->db->insert_id();

            unset($data['_filepath']);
            unset($data['_type']);

            /* INSERT ARTICLE */
            $this->db->insert('article',$data);

            $articleid = $this->db->insert_id();

            /* INSERT ARTICLE MEDIA */
            $this->db->insert('articlemedia',array('article_id'=>$articleid,'media_id'=>$mediaid)); 

            redirect(base_url());

        }else{

            $msg = validation_errors();

            echo json_encode(array('action' => $action, 'msg' => $msg));

        }
    }

    function get_article_json(){
        echo json_encode(array('arr'=>$this->art->art_id($_POST['id'])));
    }

    function valid_new_form(){

        $this->load->library('form_validation');        

        $this->form_validation->set_rules('uptitle', 'Nad naslov', 'required');
        $this->form_validation->set_rules('title', 'Naslov', 'required');
        $this->form_validation->set_rules('text', 'Tekst', 'required');


        $this->form_validation->set_message('required','Molimo unesite <b>%s</b>!');


        if ($this->form_validation->run() == FALSE)
        {                
            return FALSE;
        }
        else
        {
            return TRUE;
        }

    }

    function valid_media_form(){

        $this->load->library('form_validation');        

        $this->form_validation->set_rules('uptitle', 'Nad naslov', 'required');
        $this->form_validation->set_rules('title', 'Naslov', 'required');
        $this->form_validation->set_rules('text', 'Tekst', 'required');
        $this->form_validation->set_rules('_filepath', 'medij', 'required');
        $this->form_validation->set_rules('_type', 'tip medija', 'required');


        $this->form_validation->set_message('required','Molimo odaberite <b>%s</b>!');


        if ($this->form_validation->run() == FALSE)
        {                
            return FALSE;
        }
        else
        {
            return TRUE;
        }

    } 

}